package com.cn.qgx428.orderingmeal;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.myClass.User;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class SignIn extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "SignInActivity";

    private EditText account;
    private EditText password;
    private EditText name;
    private EditText phone;
    private EditText address;

    private ImageView backToLogin;
    private TextView signIn;
    private CheckBox agree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);

        Bmob.initialize(this,"1b925fe2ccbb402f4034a3e28c31ad93");
        init();
        //设置EditText聚焦时无提示
        setEditTextFocusNotHint();
    }

    private void setEditTextFocusNotHint() {
        account.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                EditText _v=(EditText) view;
                if (!hasFocus) {// 失去焦点
                    _v.setHint(_v.getTag().toString());
                } else {
                    String hint=_v.getHint().toString();
                    _v.setTag(hint);
                    _v.setHint("");
                }
            }
        });

        password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasfocus) {
                EditText _v=(EditText)view;
                if (!hasfocus){
                    _v.setHint(_v.getTag().toString());
                } else{
                    String hint = _v.getHint().toString();
                    _v.setTag(hint);
                    _v.setHint("");
                }
            }
        });

        name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasfocus) {
                EditText _v=(EditText)view;
                if (!hasfocus){
                    _v.setHint(_v.getTag().toString());
                } else{
                    String hint = _v.getHint().toString();
                    _v.setTag(hint);
                    _v.setHint("");
                }
            }
        });

        phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasfocus) {
                EditText _v=(EditText)view;
                if (!hasfocus){
                    _v.setHint(_v.getTag().toString());
                } else{
                    String hint = _v.getHint().toString();
                    _v.setTag(hint);
                    _v.setHint("");
                }
            }
        });

        address.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasfocus) {
                EditText _v=(EditText)view;
                if (!hasfocus){
                    _v.setHint(_v.getTag().toString());
                } else{
                    String hint = _v.getHint().toString();
                    _v.setTag(hint);
                    _v.setHint("");
                }
            }
        });
    }

    private void init() {
        //用户信息
        account = (EditText) findViewById(R.id.signIn_account);
        password = (EditText) findViewById(R.id.signIn_password);
        name = (EditText) findViewById(R.id.signIn_name);
        phone = (EditText) findViewById(R.id.signIn_phone);
        address = (EditText) findViewById(R.id.signIn_address);

        //功能控件
        backToLogin = (ImageView) findViewById(R.id.signIn_back);
        backToLogin.setOnClickListener(this);
        signIn = (TextView) findViewById(R.id.signIn_signIn);
        signIn.setOnClickListener(this);
        agree =  (CheckBox) findViewById(R.id.signIn_agree);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.signIn_back:
                finish();
                break;
            case R.id.signIn_signIn:
                sign();
                break;
        }
    }

    private void sign() {
        if (agree.isChecked()){
            User user = new User();
            user.setUsername(account.getText().toString());
            user.setPassword(password.getText().toString());
            user.setName(name.getText().toString());
            user.setMobilePhoneNumber(phone.getText().toString());
            user.setAddress(address.getText().toString());
            user.signUp(new SaveListener<User>() {
                @Override
                public void done(User user, BmobException e) {
                    if (e == null){
                        Toast.makeText(SignIn.this, "注册成功", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(SignIn.this,Login.class));
                        finish();
                    } else{
                        if (e.getErrorCode() == 202){
                            Toast.makeText(SignIn.this, "账号已注册", Toast.LENGTH_SHORT).show();
                        } else {
                            Log.d(TAG, "done: "+e);
                        }
                    }
                }
            });
        } else{
            Toast.makeText(this, "拒绝协议，无法注册", Toast.LENGTH_SHORT).show();
        }
    }
}
